package com.zmc.coupon.infrastructure.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zmc.common.utils.StringUtils;
import com.zmc.common.utils.token.AuthTokenService;
import com.zmc.coupon.infrastructure.bean.query.ReductionQuery;
import com.zmc.coupon.infrastructure.service.SkuFullReductionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zmc.common.utils.PageUtils;

import com.zmc.coupon.infrastructure.dao.SkuFullReductionDao;
import com.zmc.coupon.infrastructure.entity.SkuFullReductionEntity;


/**
 * @author zmc
 */
@Service("skuFullReductionService")
public class SkuFullReductionServiceImpl extends ServiceImpl<SkuFullReductionDao, SkuFullReductionEntity> implements SkuFullReductionService {
    @Autowired
    private AuthTokenService authTokenService;
    @Override
    public PageUtils queryPage(ReductionQuery query) {
        return new PageUtils(baseMapper.selectPage(new Page<>(query.getPageNum(), query.getPageSize()), getWrapper(query)));
    }

    @Override
    public List<SkuFullReductionEntity> queryList(ReductionQuery query) {
        return list(getWrapper(query));
    }

    @Override
    public List<SkuFullReductionEntity> getBySkuIds(List<String> skuIds) {
        LambdaQueryWrapper<SkuFullReductionEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(SkuFullReductionEntity::getSkuId, skuIds);
        return list(wrapper);
    }

    @Override
    public List<String> getListByMerchantId(String merchantId) {
        LambdaQueryWrapper<SkuFullReductionEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SkuFullReductionEntity::getMerchantId, merchantId);
        return list(wrapper).stream().map(SkuFullReductionEntity::getSkuId).collect(Collectors.toList());
    }

    @Override
    public SkuFullReductionEntity getBySkuId(String skuId) {
        LambdaQueryWrapper<SkuFullReductionEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SkuFullReductionEntity::getSkuId, skuId);
        return getOne(wrapper);
    }


    private LambdaQueryWrapper<SkuFullReductionEntity> getWrapper(ReductionQuery query) {
        LambdaQueryWrapper<SkuFullReductionEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SkuFullReductionEntity::getMerchantId, authTokenService.getUserInfo().getMerchantId())
                .eq(StringUtils.isNotEmpty(query.getSpuName()), SkuFullReductionEntity::getSpuName, query.getSpuName())
                .eq(StringUtils.isNotEmpty(query.getSkuId()), SkuFullReductionEntity::getSkuId, query.getSkuId())
                .orderByDesc(SkuFullReductionEntity::getCreateTime);
        return wrapper;
    }



}